﻿/********************************************************
 * 类名： InitPurDetailDAL.cs
 * 功能： 购货单明细数据层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-12-20	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using PSS.Entity.InitPurManage;
using PSS.Utility;

namespace PSS.DAL.InitPurManage
{
    public class InitPurDetailDAL : DbHelperBase
    {
        public void AddInitPurDetail(List<InitPurDetailEntity> list)
        {
            var conn = Db.GetConnection() as SqlConnection;
            if (conn == null)
            {
                throw new InvalidOperationException("No Valid Database Connection!");
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            using (var bcp = new SqlBulkCopy(conn))
            {
                var mapInitPurDetailId =
                    new SqlBulkCopyColumnMapping("InitPurDetailId", "InitPurDetailId");
                bcp.ColumnMappings.Add(mapInitPurDetailId);

                var mapInitPurId =
                    new SqlBulkCopyColumnMapping("InitPurId", "InitPurId");
                bcp.ColumnMappings.Add(mapInitPurId);

                var mapGoodsId =
                    new SqlBulkCopyColumnMapping("GoodsId", "GoodsId");
                bcp.ColumnMappings.Add(mapGoodsId);

                var mapQty =
                    new SqlBulkCopyColumnMapping("Qty", "Qty");
                bcp.ColumnMappings.Add(mapQty);

                var mapPurchasePrice =
                   new SqlBulkCopyColumnMapping("PurchasePrice", "PurchasePrice");
                bcp.ColumnMappings.Add(mapPurchasePrice);

                var mapDiscountRate =
                   new SqlBulkCopyColumnMapping("DiscountRate", "DiscountRate");
                bcp.ColumnMappings.Add(mapDiscountRate);

                var mapDeductionAmount =
                   new SqlBulkCopyColumnMapping("DeductionAmount", "DeductionAmount");
                bcp.ColumnMappings.Add(mapDeductionAmount);

                var mapPurchaseAmount =
                   new SqlBulkCopyColumnMapping("PurchaseAmount", "PurchaseAmount");
                bcp.ColumnMappings.Add(mapPurchaseAmount);

                var mapStorageId =
                   new SqlBulkCopyColumnMapping("StorageId", "StorageId");
                bcp.ColumnMappings.Add(mapStorageId);

                bcp.DestinationTableName = "PSS_InitPurDetail";

                try
                {
                    bcp.WriteToServer(Collection.ToDataTable(list));
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
        }

        public bool DeleteInitPurDetail(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" DELETE FROM [PSS_InitPurDetail] WHERE InitPurId=@InitPurId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@InitPurId", id);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public IList<InitPurDetailEntity> GetInitPurDetailList(InitPurDetailQcEntity qcmodel)
        {
            var list = new List<InitPurDetailEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT A.[InitPurDetailId],A.[InitPurId],A.[GoodsId],B.[GoodsCode],B.[GoodsName],D.[UnitName],A.[Qty],A.[PurchasePrice],A.[DiscountRate],A.[DeductionAmount],A.[PurchaseAmount],A.[StorageId],C.[StorageCode],C.[StorageName],B.[Specific] FROM [PSS_InitPurDetail] AS A  ");
            strSql.Append(" LEFT JOIN PSS_Goods AS B ON A.GoodsId=B.GoodsId ");
            strSql.Append(" LEFT JOIN PSS_Storage AS C ON A.StorageId=C.StorageId ");
            strSql.Append(" LEFT JOIN PSS_Unit AS D ON B.UnitId=D.UnitId ");
            strSql.Append(" WHERE A.[InitPurId]=@InitPurId ");
            strSql.Append(" ORDER BY A.CreateTime ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@InitPurId", qcmodel.InitPurId);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new InitPurDetailEntity();
                            if (reader["InitPurDetailId"] != DBNull.Value)
                                model.InitPurDetailId = new Guid(reader["InitPurDetailId"].ToString());
                            if (reader["InitPurId"] != DBNull.Value)
                                model.InitPurId = new Guid(reader["InitPurId"].ToString());
                            if (reader["GoodsId"] != DBNull.Value)
                                model.GoodsId = new Guid(reader["GoodsId"].ToString());
                            model.GoodsCode = reader["GoodsCode"] == obj ? null : Convert.ToString(reader["GoodsCode"]);
                            model.GoodsName = reader["GoodsName"] == obj ? null : Convert.ToString(reader["GoodsName"]);
                            model.UnitName = reader["UnitName"] == obj ? null : Convert.ToString(reader["UnitName"]);
                            model.Specific = reader["Specific"] == obj ? null : Convert.ToString(reader["Specific"]);
                            if (reader["Qty"] != DBNull.Value)
                                model.Qty = Convert.ToDecimal(reader["Qty"].ToString());
                            if (reader["PurchasePrice"] != DBNull.Value)
                                model.PurchasePrice = Convert.ToDecimal(reader["PurchasePrice"].ToString());
                            if (reader["DiscountRate"] != DBNull.Value)
                                model.DiscountRate = Convert.ToDecimal(reader["DiscountRate"].ToString());
                            if (reader["DeductionAmount"] != DBNull.Value)
                                model.DeductionAmount = Convert.ToDecimal(reader["DeductionAmount"].ToString());
                            if (reader["PurchaseAmount"] != DBNull.Value)
                                model.PurchaseAmount = Convert.ToDecimal(reader["PurchaseAmount"].ToString());
                            if (reader["StorageId"] != DBNull.Value)
                                model.StorageId = new Guid(reader["StorageId"].ToString());
                            model.StorageCode = reader["StorageCode"] == obj ? null : Convert.ToString(reader["StorageCode"]);
                            model.StorageName = reader["StorageName"] == obj ? null : Convert.ToString(reader["StorageName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
